﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Business.Stores;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Presentation.Stores.Presenters
{
    public class ProductStockMassivePresenter
    {
        private IViews.IProductStockMassive vManagedView;

        public ProductStockMassivePresenter(IViews.IProductStockMassive managedView)
        {
            if (managedView == null)
            {
                throw new Exception("managedView cannot be null");
            }

            vManagedView = managedView;
            vManagedView.OnLoad += new EventHandler(vManagedView_OnLoad);
            vManagedView.OnEdit += new IViews.OnEntityEventHandler(vManagedView_OnEdit);
            vManagedView.OnDelete += new IViews.OnEntityEventHandler(vManagedView_OnDelete);
            vManagedView.OnSave += new IViews.OnSaveEventHandler(vManagedView_OnSave);
            vManagedView.OnChargeProductPrice += new EventHandler(vManagedView_OnChargeProductPrice);

        }

        void vManagedView_OnChargeProductPrice(object sender, EventArgs e)
        {
            ProductPriceManager vProductPriceManager = new ProductPriceManager();
            List<ProductPrice> colProductPrice = new List<ProductPrice>();
            if (vManagedView.idProduct>=0 && vManagedView.idSupplier>=0)
            {
                ProductPrice objProductPrice = vProductPriceManager.GetProductPriceByProductAndSupplier(
                vManagedView.idProduct, vManagedView.idSupplier);
                colProductPrice.Add(objProductPrice);
            }
            vManagedView.colProductPrice = colProductPrice;
        }

        void vManagedView_OnSave(object sender, bool isNew)
        {
            ProductStockManager vProductStockManager = new ProductStockManager();
            if (isNew)
            {
                vProductStockManager.SaveProductStock(vManagedView.objProductStock);
            }
            else
            {
                vProductStockManager.UpdateProductStock(vManagedView.objProductStock);
            }
            vManagedView.colProductStockMassive = vProductStockManager.GetProductStock();
        }

        void vManagedView_OnDelete(object sender, long id)
        {
            ProductStockManager vProductStockManager = new ProductStockManager();
            vProductStockManager.DeleteProductStock(id);

            vManagedView.colProductStockMassive = vProductStockManager.GetProductStock();
        }

        void vManagedView_OnEdit(object sender, long id)        
        {
            ProductStockManager vProductStockManager = new ProductStockManager();
            vManagedView.objProductStock = vProductStockManager.GetProductStockByID(id);

            vManagedView.colProductStockMassive = vProductStockManager.GetProductStock();
        }

        void vManagedView_OnLoad(object sender, EventArgs e)
        {
            ProductStockManager vProductStockManager = new ProductStockManager();
            vManagedView.colProductStockMassive = vProductStockManager.GetProductStock();

            ProductManager vProductManager = new ProductManager();
            vManagedView.colProduct = vProductManager.GetProducts();

            SupplierManager vSupplierManager = new SupplierManager();
            vManagedView.colSupplier = vSupplierManager.GetSuppliers();

            StoreManager vStoreManager = new StoreManager();
            vManagedView.colStore = vStoreManager.GetStoresActives();
        }

    }
}
